package cn.enzo.yun.controller;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.enzo.utils.controller.CommonController;
import cn.enzo.yun.domain.Sale;
import cn.enzo.yun.service.ISaleService;
import cn.enzo.yun.vo.BoostrapVo;
import cn.enzo.yun.vo.GridVo;
import cn.enzo.yun.vo.PagerPropertyUtils;
import cn.enzo.yun.vo.RequestGrid;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = SaleController.url_path)
public class SaleController extends CommonController<Sale> {

	public final static String url_path = "/sale";
	private static final Logger logger = Logger.getLogger(SaleController.class);

	@Autowired
	private ISaleService saleService;

	@RequestMapping(value = "view")
	public String view() {
		System.out.println(" view ... ");
		return "sale/salelist";
	}
	@RequestMapping(value = "view2")
	public String view2() {
		System.out.println(" view2 ... ");
		return "sale/salelist2";
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public GridVo list(ModelAndView mav, String gridPager) throws Exception {

		GridVo gridVo = new GridVo();

		logger.info("sale list ...");
		GridVo pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager));
		PageHelper.startPage(pager.getNowPage(), pager.getPageSize());
		Sale sale = new Sale();
		List<Sale> storelist = saleService.queryByCondition(sale);
		PageInfo<Sale> pageInfo = new PageInfo<>(storelist);
		gridVo.setExhibitDatas(pageInfo.getList());
		gridVo.setPageSize(pageInfo.getPageSize());
		gridVo.setNowPage(pageInfo.getPageNum());
		gridVo.setRecordCount(pageInfo.getTotal());
		gridVo.setPageCount(pageInfo.getPages());
		gridVo.setIsSuccess(true);

		// mav.addObject(PAGE, pageInfo);
		// mav.setViewName("sale/salelist");
		return gridVo;
	}

	@ResponseBody
	@RequestMapping(value = "boostrap")
	public BoostrapVo boostraplist(HttpServletRequest request,@RequestBody RequestGrid requestGrid ) {

		try {
//			Integer parameter = Integer.valueOf(request.getParameter("offset"));
//			Integer limit = Integer.valueOf(request.getParameter("limit"));
			PageHelper.startPage(requestGrid.getOffset() / requestGrid.getLimit() + 1, requestGrid.getLimit());
			Sale sale = new Sale();
			List<Sale> storelist = saleService.queryByCondition(sale);
			PageInfo<Sale> pageInfo = new PageInfo<>(storelist);
			BoostrapVo boostrapVo = new BoostrapVo();
			boostrapVo.setRows(storelist);
			boostrapVo.setTotal(pageInfo.getTotal()+ 40);
			return boostrapVo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}

	@RequestMapping(value = "/delete")
	public ModelAndView delete(ModelAndView mav, String id, Integer pageNum) {

		logger.info("sale delete ...");
		try {
			saleService.deleteBykey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mav.setViewName(REDIRECT + url_path + "/list?pageNum=" + pageNum);
		return mav;
	}
	
}
